package com.redoor.iot.exception;

import com.redoor.iot.exception.UserException;
import com.redoor.iot.model.Result;
import com.redoor.iot.properties.ExceptionProperties;
import com.redoor.iot.utils.ResultUtil;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
org.apache.shiro.authz.UnauthenticatedException
 */
@ControllerAdvice
public class ExceptionHandle {

    private final static Logger logger = LoggerFactory.getLogger(ExceptionHandle.class);
    
    @Autowired
    private ExceptionProperties exceptionProperties;

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result<?> handle(Exception e) {
        if (e instanceof UserException) {
        	UserException userException = (UserException) e;
            return ResultUtil.error(userException.getCode(), userException.getMessage());
        }else {
            logger.error("【系统异常】", e);
            return ResultUtil.error(-1, exceptionProperties.getSystemError());
        }
    }
}
